package com.xoa.service.workflow.flowtigger.impl;

import com.xoa.dao.attend.AttendLeaveMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.dao.workflow.FlowTiggerMapper;
import com.xoa.model.attend.AttendLeave;
import com.xoa.model.workflow.FlowTiggerModel;
import com.xoa.service.workflow.flowtigger.FlowTiggerService;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import org.apache.axis.client.async.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建作者:   张勇
 * 创建日期:   2017/6/7 14:10
 * 类介绍  :
 * 构造参数:
 */
@Service
public class FlowTiggerServiceImpl implements FlowTiggerService {

    @Resource
    private FlowTiggerMapper flowTiggerMapper;

    @Resource
    private FlowProcessMapper processMapper;


    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 14:11
     * 方法介绍:   新建触发器
     * 参数说明:  触发器参数对象
     * @return
     */
    @Override
    @Transactional
    public ToJson<FlowTiggerModel> saveTigger(FlowTiggerModel flowTiggerModel) {
        ToJson<FlowTiggerModel> toJson = new ToJson<FlowTiggerModel>();
        List<FlowTiggerModel> returnList = new ArrayList<FlowTiggerModel>();
        try {
            if(flowTiggerModel.getIsGlobal() == 0) {
                flowTiggerMapper.save(flowTiggerModel);
                toJson.setObject(flowTiggerModel);
            }else {
                // TODO 全局设置
                List<Integer> list = processMapper.selectShowPrcsId(flowTiggerModel.getFlowId());
                for(int i = 0 , len = list.size();i<len;i++){
                    flowTiggerModel.setFlowPrcs(list.get(i));
                    flowTiggerMapper.save(flowTiggerModel);
                    returnList.add(flowTiggerModel);
                }
                toJson.setObj(returnList);
            }
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            L.e("FlowTiggerModel_SaveError:"+e);
            toJson.setFlag(1);
            toJson.setMsg("error");
        }
        return toJson;
    }

    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 14:11
     * 方法介绍:   修改触发器内容
     * 参数说明:   触发器参数对象，id必传
     * @return
     */
    @Override
    @Transactional
    public ToJson<FlowTiggerModel> updateTigger(FlowTiggerModel flowTiggerModel) {
        ToJson<FlowTiggerModel> toJson = new ToJson<FlowTiggerModel>();
        try {
            //获取该流程下面所有的触发器
            List<FlowTiggerModel> list = flowTiggerMapper.selectTiggerId(flowTiggerModel.getFlowId());
            //得到list的数量
            int listLength = list.size();
            //如果传来的触发器的是局部的
            if(flowTiggerModel.getIsGlobal() == 0){

                if(listLength==1){
                    flowTiggerMapper.update(flowTiggerModel);
                }else{
                    // TODO 如果全局转为局部需要删除其他节点触发器
                    List<Integer> listProcess = processMapper.selectShowPrcsId(flowTiggerModel.getFlowId());
                    for (int i = 0 , len = listProcess.size(); i < len ; i ++){
                        if(listProcess.get(i) != flowTiggerModel.getFlowPrcs()){
                            // 删除除本节点触发器的数据
                            flowTiggerMapper.deleteTigger(list.get(i).getId());
                        }else {
                            //修改本节点触发器的数据
                            flowTiggerMapper.update(flowTiggerModel);
                        }
                    }
                }
            }else {
                //如果是全局的
                  //判断数量是不是一个
                if (listLength == 1) {
                    // 获取当前节点触发器ID
                    int flowPrcsId = flowTiggerModel.getId();
                    // 获取页面传递流程步骤
                    int flowPrcs = flowTiggerModel.getFlowPrcs();
                    //对应flowId查询所有流程步骤的ID串
                    List<Integer> listProcess = processMapper.selectShowPrcsId(flowTiggerModel.getFlowId());
                    //集合便利
                    for (int i = 0, len = listProcess.size(); i < len; i++) {
                        int flowProcessId = listProcess.get(i);
                            if (flowProcessId != flowPrcs) {
                            flowTiggerModel.setFlowPrcs(flowProcessId);
                            flowTiggerMapper.save(flowTiggerModel);
                        } else {
                            flowTiggerModel.setId(flowPrcsId);
                                flowTiggerModel.setFlowPrcs(flowPrcs);
                            flowTiggerMapper.update(flowTiggerModel);

                        }
                    }
                } else {

                    for(int i = 0 ; i < listLength; i ++){
                        // 获取当前节点触发器ID
                        int flowPrcsId = list.get(i).getId();
                        // 获取页面传递流程步骤
                        int flowPrcs = list.get(i).getFlowPrcs();
                        //对应flowId查询所有流程步骤的ID串
                        List<Integer> listProcess = processMapper.selectShowPrcsId(flowTiggerModel.getFlowId());
                       int  len = listProcess.size();
                        for (int j = 0; j < len; j++) {
                            int flowProcessId = listProcess.get(j);
                            if (flowProcessId == flowPrcs) {
                                flowTiggerModel.setId(flowPrcsId);
                                flowTiggerModel.setFlowPrcs(flowProcessId);
                                flowTiggerMapper.update(flowTiggerModel);
                            }

                        }


                    }
                }
            }
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            L.e("FlowTiggerModel_UpdateError:"+e);
            toJson.setFlag(1);
            toJson.setMsg("error");
        }
        return toJson;
    }

    /**
     * 创建作者:   张勇
     * 创建日期:   2017/6/7 20:47
     * 方法介绍:   根据ID删除数据
     * 参数说明:
     * @return
     */
    @Override
    @Transactional
    public ToJson<FlowTiggerModel> deleteTigger(Integer id) {
        ToJson<FlowTiggerModel> toJson = new ToJson<FlowTiggerModel>();
        try{
            flowTiggerMapper.deleteTigger(id);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            toJson.setFlag(1);
            toJson.setMsg("error");
        }
        return toJson;
    }
    @Autowired
    private AttendLeaveMapper attendLeaveMapper;

    private static final String attend_leave_tableName="attend_leave";
    private static final String attend_evection_tableName="attend_evection";

    @Override
    public ToJson<FlowTiggerModel> modifyFieldProperties(String tableName, String id, Integer runId) {

        ToJson<FlowTiggerModel> flowTiggerModelToJson=new ToJson<FlowTiggerModel>();
        flowTiggerModelToJson.setMsg("err");
        flowTiggerModelToJson.setFlag(1);

        if(StringUtils.checkNull(tableName)){
            flowTiggerModelToJson.setMsg("缺少必要的请求参数：tableName");
            flowTiggerModelToJson.setFlag(1);
            return flowTiggerModelToJson;
        }
        if(attend_leave_tableName.equals(tableName)){
            AttendLeave attendLeave=new AttendLeave();
            attendLeave.setLeaveId(Integer.parseInt(id));
            attendLeave.setRunId(runId);
            if(attendLeaveMapper.updateAttendLeave(attendLeave)>0){
                flowTiggerModelToJson.setMsg("ok");
                flowTiggerModelToJson.setFlag(0);
            }

        }else if(attend_evection_tableName.equals(tableName)){

        }

        return flowTiggerModelToJson;
    }
}
